//
//  ValidateUtil.m
//  ScholarsBridge
//
//  Created by 李亚琪 on 2016/11/25.
//  Copyright © 2016年 mingthink. All rights reserved.
//

#import "ValidateUtil.h"

@implementation ValidateUtil

+(BOOL)validateTelphoneNum:(NSString *)telNum{
    NSString *mobileRegex = @"^1(3[0-9]|4[57]|5[0-35-9]|7[0135678]|8[0-9])\\d{8}$";
    return [self validateReg:mobileRegex content:telNum];
}

+ (BOOL) validateEmail:(NSString *)email
{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    return [self validateReg:emailRegex content:email];
}

+ (BOOL) validateReg:(NSString *)reg content:(NSString*)content{
    NSPredicate *mobileTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",reg];
    return [mobileTest evaluateWithObject:content];
}

+ (BOOL)validateNumber:(NSString*)number {
    BOOL res = YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    int i = 0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
    }
    return res;
}

+ (BOOL)isValidPostalCode:(NSString *)code {
    NSString *postalRegex = @"^[0-9]{4}$";
    return [self validateReg:postalRegex content:code];
}

+ (BOOL) validateIdentityCard: (NSString *)identityCard {
    BOOL flag;
    if (identityCard.length <= 0) {
        flag = NO;
        return flag;
    }
    NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
    NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
    return [identityCardPredicate evaluateWithObject:identityCard];
}

+ (BOOL) validateExamNum: (NSString *)examNum {
    NSString *postalRegex = @"^[0-9]{10,}$";
    return [self validateReg:postalRegex content:examNum];
}
+ (BOOL)isValidURL:(NSString *)urlStr {
    
    if (![urlStr containsString:@"http://"]&&![urlStr containsString:@"https://"]) {
        urlStr=[NSString stringWithFormat:@"http://%@",urlStr];
    }
    NSError *error;
    
    // 正则1
    
    NSString *regulaStr =@"\\bhttps?://[a-zA-Z0-9\\-.]+(?::(\\d+))?(?:(?:/[a-zA-Z0-9\\-._?,'+\\&%$=~*!():@\\\\]*)+)?";
    
//    // 正则2
//    regulaStr =@"((http[s]{0,1}|ftp)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|(www.[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)";
    
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regulaStr
                                                                          options:NSRegularExpressionCaseInsensitive
                                                                            error:&error];
    NSArray *arrayOfAllMatches = [regex matchesInString:urlStr options:0 range:NSMakeRange(0,[urlStr length])];
    
    for (NSTextCheckingResult *match in arrayOfAllMatches){
        NSString *substringForMatch = [urlStr substringWithRange:match.range];
        DeLog(@"-----匹配的URL:%@",substringForMatch);
        return YES;
    }
    
    return NO;
}
@end
